#ifndef WIDGET_H
#define WIDGET_H
#include "sersocket.h"
#include "localdb.h"
#include <QWidget>
#include <QQueue>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE



class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

signals:
    //client's status update
    void ConnectToServer(const QString &address, quint16 port);
    void Online(const QByteArray& info,const uint16_t cmd);
    void Connected(const QString & adddress,const uint16_t cmd);
    void Disconnected(const QString & adddress,quint16 port);

    void heart_jump(const QByteArray& uid);

    void ClientRcvPath(const QString& dir,const QString& name,const QString& id);

    //sent message
    void Text(const QByteArray& data,const uint16_t cmd);
    void FilePath(const file_box& fbox);

private slots:
    //client's status update(ui)
    void on_btnconnect_clicked();
    void on_btnlog_clicked();
    void on_btnsign_clicked();
    void on_btnstop_clicked();
    void on_btnclean_clicked();


    void up_date();

    //sent messeage
    void on_btntext_clicked();
    void update_msgid(const int& id);
    void on_btnfile_clicked();
//    void senttext();
//    void sentfile();

    //receive message
    void getList(const QJsonArray &l_data,const QJsonArray &g_data);
    void getUpdateList(const QJsonArray &l_data,const QJsonArray &g_data);
    void getUpdateMessage(const QJsonArray &msg_data);
    void getUpdateFileLists(const QJsonArray &msg_data);

    void on_btngetfile_clicked();


    //tools

    void on_connected();
    void on_disconnected();

    void on_statechange(QAbstractSocket::SocketState socketState);

private:
    bool online = false;
    bool onsign = false;
    int jump = 1;
    Ui::Widget *ui;
    SerSocket *m_socket;

    bool notsend = false;
    bool sfile_finish = true;
    bool smsg_finish = true;
    bool update_finish = false;

    //self information
    QString use_ip;
    QString use_uid;
    QString use_name;
    QJsonObject* sign_info;

    //friend information
    QString now_uid;
    QString now_msg;

    QJsonObject* id_list;
    QJsonObject* group_list;
    QJsonObject* name_list;

    //ram
    QQueue<Message> msg_que;

//    QQueue<QByteArray> msg_que;
    QQueue<file_box> file_que;

    QTimer* Clock;
};

#endif // WIDGET_H
